package wwl.lsf;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title
 * @Author wangwenliang
 * @Date 2021/10/11
 * @Description
 */
public class Z字形变换 {


    class Solution {
        public String convert(String s, int numRows) {
            if (numRows < 2) {
                return s;
            }
            List<StringBuilder> rows = new ArrayList<StringBuilder>();
            for (int i = 0; i < numRows; i++) {
                rows.add(new StringBuilder());
            }
            int i = 0, flag = -1;
            for (char c : s.toCharArray()) {
                rows.get(i).append(c);
                if (i == 0 || i == numRows - 1) {
                    flag = -flag;
                }
                i += flag;
            }
            StringBuilder res = new StringBuilder();
            for (StringBuilder row : rows) {
                res.append(row);
            }
            return res.toString();
        }

        public String myConcert(String s, int numRows) {

            if (numRows < 2) {
                return s;
            }
            List<StringBuilder> stringBuilders = new ArrayList<>();

            for (int i = 0; i < numRows; i++) {
                stringBuilders.add(new StringBuilder());
            }

            int i = 0, flag = -1;
            for (char c : s.toCharArray()) {
                stringBuilders.get(i).append(c);
                if (i == 0 || i == numRows - 1) {
                    flag = -flag;
                }
                i += flag;
            }

            StringBuilder result = new StringBuilder();

            for (StringBuilder stringBuilder : stringBuilders) {
                result.append(stringBuilder);
            }

            return result.toString();
        }


    }

}
